'''
Created on 2009-12-9

@author: Jialai_Zhu
'''
import urllib
import binascii 
import re
x = r'''var u=YT("%u7468%u7074%u2F3A%u642F%u776F%u2E6E%u3131%u6734%u6172%u6870%u632E%u6D6F%u622F%u2F62%u6573%u7672%u7265%u652E%u6578%u0000"); '''
def unescapeA(str):
    ret = ''
    for c in str.split("%u"):         
        try:
            a = binascii.a2b_hex(c)
            ret += a[::-1]                     
        except:pass
    return ret     

    
def e4c(_a):
    try:
        p = re.compile(r'["\']%u(.*?)["\']')
        ret = p.findall(_a)
#        .groups()
        out = '%u'
        for i in ret:
            out += i
        return out
    except Exception, ex:
        return None
    

def unescape(data):
    ret=''
    for x in data.split("%u"):
        try:
            if len(x)==4:
                ret+= binascii.a2b_hex(x)[::-1]
            if len(x)==2:
                ret+= binascii.a2b_hex(x)
        except:
            pass
    return ret

            
def unescapeX(data):
    ret=''
    for x in data.split("\\x"):
        try:
            ret+=binascii.a2b_hex(x)
        except:
            ret+='\0'
    if ret=='':    
        return None
    return ret

def xstrcat(data):
    ret=''
    for c in data.split("%u"):
        try:
            if len(c)==4:
                ret+= "\\x"+c[2:]+"\\x"+c[0:2]
            if len(c)==2:
                ret+= "\\x"+c[0:2]
        except:
            pass
    if ret=='':    
        return None
    return ret

        


xxx='%ud5db%uC9C9%u87CD%u9292%uCDCD%u93DA%uC8C8%u93DF%uDEDE%uD192%uC592%u93C5%uC5D8%uBDD8%uBD"'
#print unescapeX(xxx)    
#        
#    
#import link
#print link.http1(unescapeA(e4c(x)))
    

        
#print normalize_url("www.sina.com.cn///")        
